package com.example.library.controller;

import com.example.library.constant.Constants;
import com.example.library.model.UserInfo;
import com.example.library.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: asus
 * Date: 2024-12-16
 * Time: 23:23
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    public UserService userService;
    @RequestMapping("/login")
    public Boolean login(String userName, String password, HttpSession session) {
        //1. 根据用户名查找用户信息
        UserInfo userInfo = userService.getUserInfoByname(userName);
        //2. 比对密码是否正确
        if (userInfo == null || userInfo.getId() <= 0) {
            return false;
        }
        if (password.equals(userInfo.getPassword())) {
            //密码正确;
            userInfo.setPassword("");
            session.setAttribute(Constants.SESSION_USER_KEY, userInfo);
            return true;
        }
        return false;
    }
    @RequestMapping(value = "/register", produces = "application/json")
    public String register(String userName, String password, HttpSession session) {
        if (userName == null || password == null) {
            return "参数有误, 请重新输入";
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName(userName);
        userInfo.setPassword(password);
        Integer result = userService.insertUserInfo(userInfo);
        if (result <= 0) {
            return "用户名重复, 请重新注册";
        }
//        userInfo.setPassword("");
//        session.setAttribute(Constants.SESSION_USER_KEY, userInfo);
        return "";
    }
}
